package com.lib.listener;

import com.lib.application.RentalAgentService;
import com.lib.application.CustomerService;
import com.lib.application.WxAccountService;
import com.lib.model.WxAccount;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import com.lib.listener.event.*;
/**
 *
 *
 *
 * Created by duqingxiang on 17/5/22.
 */
@Component
public class RegisterListener {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    CustomerService customerService;

    @Autowired
    RentalAgentService rentalAgentService;

    @Autowired
    WxAccountService wxAccountService;

    @EventListener
    public void register(RegisterEvent event) {
        logger.info("customerLister {}",event);

        WxMpUser userinfo = event.getUserInfo();


        WxAccount wxAccount = wxAccountService.findWxAccountByOpenId(userinfo.getOpenId());

        if (wxAccount != null) {
            return;
        }

        wxAccount = WxAccount.builder()
                .openId(userinfo.getOpenId())
                .city(userinfo.getCity())
                .province(userinfo.getProvince())
                .country(userinfo.getCountry())
                .subscribe(userinfo.getSubscribe())
                .nickname(userinfo.getNickname())
                .sex(userinfo.getSex())
                .language(userinfo.getLanguage())
                .headImgUrl(userinfo.getHeadImgUrl())
                .subscribeTime(userinfo.getSubscribeTime())
                .unionId(userinfo.getUnionId())
                .sexId(userinfo.getSexId())
                .remark(userinfo.getRemark())
                .groupId(userinfo.getGroupId())
                .tagIds(userinfo.getTagIds())
                .build();

        switch (event.getTag()) {
            case CUSTOMER:
                customerService.registerCustomer(wxAccount);
                break;
            case AGENT:
                rentalAgentService.registerAgent(wxAccount);
                break;
        }



    }



}
